
//
//  VBNetworkTools.swift
//  VBo
//
//  Created by 赵世得 on 2016/10/28.
//  Copyright © 2016年 Zh. All rights reserved.
//

import UIKit
import AFNetworking

enum VBNetworkToolsMethod: String {
    case get = "get"
    case post = "post"
}

class VBNetworkTools: AFHTTPSessionManager {
    static let sharedTools:VBNetworkTools = {
        let tools = VBNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    func request(method: VBNetworkToolsMethod, urlString: String, parameter: Any?, sucsess: @escaping (Any?)->(), failure:@escaping (Error)->()){
        if method == .get  {
            get(urlString, parameters: parameter, progress: nil, success:{
                (_,responesObject) in sucsess(responesObject)
                }, failure: {
                    (_,error) in failure(error)
            })
        }else{
            post(urlString, parameters: parameter, progress: nil, success: { (_, responesObjecct) in
                sucsess(responesObjecct)
                }, failure: { (_, error) in
                    failure(error)
            })
        }
        
    }
}
extension VBNetworkTools{
    func oauthLoadUserAccount(code: String, sucsess: @escaping (Any?)->(), failure:@escaping (Error)->()){
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = [
            "client_id": VBAPPKEY,
            "client_secret": VBAPPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": VBREDIRECTURI
        ]
        request(method: VBNetworkToolsMethod.post, urlString: urlString, parameter: params, sucsess: sucsess, failure: failure)
        
    }
    
    func oauthLoadUserInfo(model: VBUserAccountModel, sucsess: @escaping (Any?)->(), failure:@escaping (Error)->()){
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = [
            "access_token":model.access_token!,
            "uid":model.uid!
        ]
        request(method: VBNetworkToolsMethod.get, urlString: urlString, parameter: params, sucsess: sucsess, failure: failure)
    }
    
    
    
    
    
    
    
    
    
}
